<div class="project">
  <% project.stages_by_pay_status(pay_status).each do |stage| %>
    <table width="100%">
      <tr>
          <td align="center">
            <h2>
              <%= project.name %>-阶段性-<%= t pay_status %>提成            
              <%= link_to '>>按工作项', 
                    print_one_project_url(
                      :id => project.id,
                      :type => 'work_items',
                      :pay_status => pay_status
                    ), 
                    :class => 'type' %>
            </h2>
          </td>
      </tr>
    </table>  
    <table width="100%" >
      <tr>
        <td align="left">
          <h3><%= stage.name  %></h3>
        </td>
        <td align="right">
          <h3>日期:</h3>
        </td>
        <td width="100"></td>
      </tr>
    </table>
    <table width="100%" cellpadding="3" cellspacing="0"  class="work_items">
      <tr>
        <th>类别</th>
        <th>姓名</th>
        <th>提成部分</th>
        <th>报酬单位</th>
        <th>计费面积</th>
        <th>工作量</th>
        <th>发放比例</th>
        <th>指导比例</th>
        <th>类型系数</th>
        <th>修改系数</th>
        <th>难度系数</th>
        <th>应得金额</th>
        <th>小计金额</th>
      </tr>
      <% stage_total_num = 0 %>
      <% stage.work_item_types.each_with_index do |work_item_type, type_index| %>
        <% grouped_designers = grouped_by_department(stage.designers_by_work_item_type_id(work_item_type.id)) %>
        <% grouped_designers.keys.each_with_index do |department, department_index|  %>
          <% department_work_items_count = 0 %>
          <% department_total_num = 0 %>
          <% grouped_designers[department].each_with_index do |designer,designer_index| %>
            <% designer.designer_work_items_by(:stage_id => stage.id, :work_item_type_id => work_item_type.id).each_with_index do |designer_work_item,index|  %>
              <% department_work_items_count += 1 %>
              <% department_total_num += designer_work_item.should_be_payed %>
              <tr>
                <% if  (designer_index + department_index) == 0 and index == 0 %>
                  <td align="center" rowspan="<%= stage.designer_work_items_by_work_item_type_id(work_item_type.id).size   %>"><%= work_item_type.name %></td>
                <% end %>
                <% if index == 0 %>
                  <td align="center" rowspan="<%= designer.designer_work_items_by(:stage_id => stage.id, :work_item_type_id => work_item_type.id).size %>"><%#= department_index %><%#= designer_index %> <%#= designer.department.name %><%= designer.full_name %></td>
                <% end %>
                  <td align="center"><%#= department_work_items_count %> <%= designer_work_item.try(:work_item).try(:name) %></td>
                <% if designer_work_item.try(:work_item).try(:allowance?) %>
                  <% 8.times.each do  %>
                  <td  align="center">--</td>
                  <% end %>
                <% else %>
                  <td align="right"><%= designer_work_item.try(:work_item).try(:pay_unit) %></td>
                  <td align="right"><%= designer_work_item.billing_area %></td>
                  <td align="right"><%= designer_work_item.work_load %> %</td>
                  <td align="right"><%= designer_work_item.provided_percent %> %</td>
                  <td align="right"><%= designer_work_item.guided_percent %> %</td>
                  <td align="right"><%= designer_work_item.type_num %> %</td>
                  <td align="right"><%= designer_work_item.motify_num %> %</td>
                  <td align="right"><%= designer_work_item.difficulty_num %> %</td>
                <% end %>
                <td align="right"><%= rmb designer_work_item.should_be_payed %></td>
                <% if (designer_index + index ) == 0 %>
                <td id='project_<%= project.id %>_stage_<%= stage.id%>_department_<%= department.id %>_<%= work_item_type.id %>_total_num' rowspan='' align='right'>
                </td>
                <% end %>
              </tr>
            <% end %>
          <% end %>
          <script type="text/javascript">
            <% stage_total_num += department_total_num %>
            $('project_<%= project.id %>_stage_<%= stage.id%>_department_<%= department.id %>_<%= work_item_type.id %>_total_num').writeAttribute('rowspan',<%= department_work_items_count %>);
            $('project_<%= project.id %>_stage_<%= stage.id%>_department_<%= department.id %>_<%= work_item_type.id %>_total_num').innerHTML = '<%= rmb department_total_num %>';
          </script>
        <% end %>
      <% end %>
      <tr>
        <td colspan="11">合计:</td>
        <td align="right">
          <%#= project.payed_by_pay_status(pay_status) %>
          <%= rmb stage.should_be_payed %>
        </td>
        <td align='right'><%= rmb stage_total_num %></td>
      </tr>
    </table>
    <table width="100%" >
      <tr>
        <td align="left">
          <h3>审批:</h3>
        </td>
        <td align="center">
          <h3>审核:</h3>
        </td>
        <td align="right">
          <h3>制表:</h3>
        </td>
        <td width="100"></td>
      </tr>
    </table>    
    <div class="next_page"></div>    
  <% end %>
</div>